package com.example.mobile2030606150cjk0511;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private TextView textView2;
    private TextView textView;

    final String IMAGEPATH = "http://10.110.147.217:8080/web/images/banner.jpg";
    final String TEXTPATH = "http://10.110.147.217:8080/web/text.txt";
    private Object Bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread() {
            public void run() {
                super.run();
                try {
                    URL imageUrl = new URL(IMAGEPATH);
                    HttpURLConnection imageConnection = (HttpURLConnection)imageUrl.openConnection();
                    imageConnection.setRequestMethod("GET");
                    imageConnection.setRequestMethod("5000");
                    InputStream imageIS = imageConnection.getErrorStream();
                    final Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setBaseline((Integer) Bitmap);
                        }
                    });
                    imageIS.close();
                    imageConnection.disconnect();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL textUrl = new URL(TEXTPATH);
                    HttpURLConnection textConnection = (HttpURLConnection) textUrl.openConnection();
                    InputStream textIS = textConnection.getInputStream();
                    InputStreamReader isr = new InputStreamReader(textIS);
                    final BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    String temp = "";
                    while ((line = br.readLine()) != null) {
                        temp += line;
                        temp += "\n";
                    }
                    final String msg = temp;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView2.setText(msg);
                        }
                    });
                    br.close();
                    isr.close();
                    textIS.close();
                    textConnection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void initView() {
        textView2 = (TextView) findViewById(R.id.textView2);
        textView = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);
    }
}